package com.twu.biblioteca;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/16/13
 * Time: 7:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class Reader {
    private String username;
    private String password;
    private ServiceDesk serviceDesk;
    private List<String> books;
    private Map<String, String> properties;

    public Reader(String username, String password) {
        this.username = username;
        this.password = password;
        properties = new HashMap<String, String>();
    }

    public Reader() {
        this(null, null);
    }

    public void enter(ServiceDesk serviceDesk) {
        this.serviceDesk = serviceDesk;
        this.serviceDesk.welcome(this);
        books = new ArrayList<String>();
    }

    public void select(String option) {
        serviceDesk.runOption(this, option);
    }

    public void reserveBook(String book) {
        serviceDesk.reserveBookFor(this, book);
    }

    public void keepBook(String book) {
        books.add(book);
    }

    public boolean hasBook(String book) {
        return books.contains(book);
    }

    public String username() {
        return username;
    }

    public String password() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String reporter() {
        StringBuilder report = new StringBuilder();
        for (Map.Entry<String, String> entry : properties.entrySet()){
            report.append(entry.getKey());
            report.append(":");
            report.append(entry.getValue());
            report.append("\n");
        }
        return report.toString();
    }

    public void setProperty(String key, String value) {
        properties.put(key, value);
    }

    public String getProperty(String key) {
        return properties.get(key);
    }

    public static Reader createReader(){
        Reader reader = new Reader("zhuran", "123456");
        reader.setProperty("name", "zhuran");
        reader.setProperty("email", "123@456.com");
        reader.setProperty("phone", "13899993333");
        return reader;
    }

    public void enterFromConsole(ServiceDesk serviceDesk) {
        this.serviceDesk = serviceDesk;
        books = new ArrayList<String>();
        this.serviceDesk.welcomeAndRun(this);
    }
}
